#include "stdafx.h"
#include "mage.h"
#include "potion.h"

#include "wrappers/selfcontrol.h"
#include <math.h>

static const float MAGE_RADIUS = 35.0f;
const int Mage::MAX_HEALTH = 100;
const int Mage::MAX_MANA = 100;
const int Mage::START_HEALTH = Mage::MAX_HEALTH;
const int Mage::START_MANA = 75;
const float Mage::SPEED_LIMIT = 2.5f;

Mage::Mage()
{
    init();
}

Mage::Mage(const vec2f &position, float direction) :
    MovingObject(position, direction)
{
    init();
}

Mage::~Mage()
{
}

void Mage::init()
{
    setRadius(MAGE_RADIUS);
    _health = START_HEALTH;
    _mana = START_MANA;
}

void Mage::drink(const Potion &potion)
{
    if (potion.kind() == Potion::MANA_POTION) {
        _mana = qMin(Mage::MAX_MANA, _mana + Potion::MANA_POTION_BONUS);
    } else {
        _health = qMin(Mage::MAX_HEALTH, _health + Potion::HEALTH_POTION_BONUS);
    }
}

int Mage::damage(int value)
{
    if (value < 0)
        return 0;
    if (value > _health)
        value = _health;

    _health -= value;
    return value;
}

int Mage::health() const
{
    return _health;
}

int Mage::mana() const
{
    return _mana;
}

const IEffectsArray *Mage::effects() const
{
    return &_effects;
}

void Mage::accept(const SelfControl *self)
{
    setDirection(direction() + self->getRotationDelta());
    float newSpeed = speed() + self->getBoostDelta();
    if (newSpeed > SPEED_LIMIT)
        newSpeed = SPEED_LIMIT;
    if (newSpeed < -SPEED_LIMIT)
        newSpeed = -SPEED_LIMIT;
    setSpeed(newSpeed);
}

void Mage::accept(ISceneObjectVisitor *visitor)
{
    visitor->visit(this);
}
